package com.zhang.zhangapp.view.shop;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.base.config.BaseConfig;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.shop.Shop;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.utils.T;
import com.zhang.zhangapp.view.BaseFragment;
import com.zhang.zhangapp.view.BaseLazyFragment;
import com.zhang.zhangapp.view.NearByFragment;
import com.zhang.zhangapp.view.adapter.ShopAdapter;
import com.zhang.zhangapp.view.custom.HeadRefreshView;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/12.
 */
@ContentView(R.layout.fragment_shop)
public class ShopFragment extends BaseFragment {

    PullToRefreshLayout refreshLayout;
    ListView listView;
    ShopAdapter shopAdapter;

    private String methodName;
    private Map<String, String> params;
    private List<Shop> shopList = new ArrayList<>();

    MCallback getHotShopCallback = new MCallback() {
        @Override
        public void onSuccess(String data) {
            Gson gson = new Gson();
            List<Shop> list = gson.fromJson(data, new TypeToken<List<Shop>>() {
            }.getType());
            if (list != null) {
                shopList.addAll(list);
            }
            shopAdapter.notifyDataSetChanged();
            if (list.size() < BaseConfig.PAGE_SIZE) {
                refreshLayout.setCanLoadMore(false);
            }
        }

        @Override
        public void onSuccess(String message, String code) {
            refreshLayout.setCanLoadMore(false);
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            refreshLayout.setCanLoadMore(false);
        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {
            refreshLayout.setCanLoadMore(false);
        }

        @Override
        public void onFinished() {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
            dismisionDalog();
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        params = (Map<String, String>) getArguments().getSerializable("params");
        methodName = getArguments().getString("methodName");
        View view = inflater.inflate(R.layout.fragment_shop, null);
        listView = (ListView) view.findViewById(R.id.shop_listview);
        refreshLayout = (PullToRefreshLayout) view.findViewById(R.id.refresh_layout);
        onFirstUserVisible();
        return view;
    }

    public void onFirstUserVisible() {
        shopAdapter = new ShopAdapter(shopList, getContext());
        listView.setAdapter(shopAdapter);
        refreshLayout.setHeaderView(new HeadRefreshView(getContext()));
        refreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                getShops(1);
            }

            @Override
            public void loadMore() {
                getShops(shopList.size() / BaseConfig.PAGE_SIZE + 1);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(ShopFragment.this.getActivity(), ShopDetailActivity.class);
                intent.putExtra("shopCode", shopList.get(i).getShopCode());
                ShopFragment.this.getActivity().startActivity(intent);
            }
        });
        getShops(1);
    }

    private void getShops(int pageNo) {
        if (pageNo == 1) {
            shopList.clear();
            shopAdapter.notifyDataSetChanged();
            refreshLayout.setCanLoadMore(true);
        }
        if (showLoadingDialog()) {
            params.put("pageNum", pageNo + "");
            CommonHttp.commonHttp(methodName, params, getHotShopCallback);
        }
    }

    public void updateParams(String key, String value) {
        params.put(key, value);
        getShops(1);
    }
}
